package ve.com.wayoyo.wuml_case.DibujoDiagramas;

import ve.com.wayoyo.wuml_case.NuevoDiagrama;
import ve.com.wayoyo.wuml_case.R;
import ve.com.wayoyo.wuml_case.Exceptions.CardinalityException;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

public class EditarCardinalidad extends Dialog  {

	private CompConector _connectorSelected;

	private Componente _compPadre;
	private Componente _compHijo;

	private LinearLayout _conector;
	private LinearLayout _padre;
	private LinearLayout _hijo;

	private RadioGroup GroupPadre = new RadioGroup(getContext());
	private RadioGroup GroupHijo = new RadioGroup(getContext());

	private EditText first_padre;
	private EditText last_padre;
	private EditText first_hijo;
	private EditText last_hijo;
	
	public EditarCardinalidad(Context context) {
		super(context);
	}

	public void alert(String mensaje) {
		Toast.makeText(this.getContext(), mensaje, Toast.LENGTH_SHORT).show();
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {	
		super.onCreate(savedInstanceState);
		setContentView(R.layout.modificar_cardinalidad_layout);
		setTitle("Modificar Conector");
		_connectorSelected = VistaDiagrama.getVista().getDiagrama()._SelectedConector;
		_compPadre = VistaDiagrama.getVista().getDiagrama().getComponenteById(_connectorSelected.get_fatherId());
		_compHijo = VistaDiagrama.getVista().getDiagrama().getComponenteById(_connectorSelected.get_childId());		
		this.getRadioGroups(VistaDiagrama.getVista().getDiagrama().getTipo());
		this.setOnCLickGuardar();
	}

	private void getRadioGroups(String tipo) {
		if (tipo.equals("Clases")){
			TextView txPadre = (TextView)findViewById(R.id.txtPadre);
			txPadre.setText(_compPadre.getTitulo());
			txPadre.setTextSize(25);
			TextView txHijo = (TextView)findViewById(R.id.txtHijo);
			txHijo.setText(_compHijo.getTitulo());
			txHijo.setTextSize(25);			
			this.setOnChangeGroupPadre(tipo);
			this.setOnChangeGroupHijo(tipo);
			this.setPrevioPadre(tipo);
			this.setPrevioHijo(tipo);
		}
	}

	private void setOnChangeGroupPadre(String tipo) {		
		final EditText etSingleNum = (EditText)findViewById(R.id.et_single_numb_padre);
		final EditText etNumAsterisco = (EditText)findViewById(R.id.et_numb_asterisco_padre);
		final EditText etNumFirst = (EditText)findViewById(R.id.et_numb_numb_first_padre);
		final EditText etNumLast = (EditText)findViewById(R.id.et_numb_numb_last_padre);
		RadioGroup rgCardinality = (RadioGroup)findViewById(R.id.radio_group_padre);	
		rgCardinality.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
			public void onCheckedChanged(RadioGroup group, int checkedId) {
				if (checkedId == R.id.rb_single_numb_padre){
					etNumAsterisco.setEnabled(false);					
					etNumFirst.setEnabled(false);					
					etNumLast.setEnabled(false);					
					etSingleNum.setEnabled(true);
					etNumAsterisco.setText("");
					etNumFirst.setText("");
					etNumLast.setText("");
					first_padre = etSingleNum;
					last_padre = etSingleNum;
				}else if(checkedId == R.id.rb_numb_asterisco_padre){
					etNumAsterisco.setEnabled(true);
					etNumFirst.setEnabled(false);					
					etNumLast.setEnabled(false);					
					etSingleNum.setEnabled(false);
					etNumFirst.setText("");
					etNumLast.setText("");
					etSingleNum.setText("");
					first_padre = etNumAsterisco;
					last_padre = new EditText(getContext());
					last_padre.setText("*");
				}else if(checkedId == R.id.rb_numb_numb_padre){
					etNumAsterisco.setEnabled(false);
					etSingleNum.setEnabled(false);
					etNumFirst.setEnabled(true);
					etNumLast.setEnabled(true);
					etNumAsterisco.setText("");
					etSingleNum.setText("");
					first_padre = etNumFirst;
					last_padre = etNumLast;
				}
			}
		});

	}

	private void setOnChangeGroupHijo(String tipo) {
		RadioGroup rgCardinality = (RadioGroup)findViewById(R.id.radio_group_hijo);	
		final EditText etSingleNum = (EditText)findViewById(R.id.et_single_numb_hijo);
		final EditText etNumAsterisco = (EditText)findViewById(R.id.et_numb_asterisco_hijo);
		final EditText etNumFirst = (EditText)findViewById(R.id.et_numb_numb_first_hijo);
		final EditText etNumLast = (EditText)findViewById(R.id.et_numb_numb_last_hijo);

		rgCardinality.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
			public void onCheckedChanged(RadioGroup group, int checkedId) {
				if (checkedId == R.id.rb_single_numb_hijo){
					etNumAsterisco.setEnabled(false);
					etNumFirst.setEnabled(false);
					etNumLast.setEnabled(false);
					etSingleNum.setEnabled(true);
					etNumAsterisco.setText("");
					etNumFirst.setText("");
					etNumLast.setText("");
					first_hijo = etSingleNum;
					last_hijo = etSingleNum;
				}else if(checkedId == R.id.rb_numb_asterisco_hijo){
					etNumAsterisco.setEnabled(true);
					etNumFirst.setEnabled(false);
					etNumLast.setEnabled(false);
					etSingleNum.setEnabled(false);
					etNumFirst.setText("");
					etNumLast.setText("");
					etSingleNum.setText("");
					first_hijo = etNumAsterisco;
					last_hijo = new EditText(getContext());
					last_hijo.setText("*");
				}else if(checkedId == R.id.rb_numb_numb_hijo){
					etNumAsterisco.setEnabled(false);
					etNumFirst.setEnabled(true);
					etNumLast.setEnabled(true);
					etSingleNum.setEnabled(true);
					etNumAsterisco.setText("");
					etSingleNum.setText("");
					first_hijo = etNumFirst;
					last_hijo = etNumLast;
				}
			}
		});

	}

	private void setPrevioPadre(String tipo) {
		RadioGroup rgCardinality = (RadioGroup)findViewById(R.id.radio_group_padre);
		RadioButton rb;

		EditText etSingleNum = (EditText)findViewById(R.id.et_single_numb_padre);
		EditText etNumAsterisco = (EditText)findViewById(R.id.et_numb_asterisco_padre);
		EditText etNumFirst = (EditText)findViewById(R.id.et_numb_numb_first_padre);
		EditText etNumLast = (EditText)findViewById(R.id.et_numb_numb_last_padre);

		if (_connectorSelected.get_fatherCardenalityLeft().equals(_connectorSelected.get_fatherCardenalityRight()))
		{
			rb = (RadioButton)rgCardinality.getChildAt(0);
			rb.setChecked(true);
			etSingleNum.setText(_connectorSelected.get_fatherCardenalityRight());
		}else if (_connectorSelected.get_fatherCardenalityRight().equals("*")){
			rb = (RadioButton)rgCardinality.getChildAt(1);
			rb.setChecked(true);
			etNumAsterisco.setText(_connectorSelected.get_fatherCardenalityLeft());
		}else{
			rb = (RadioButton)rgCardinality.getChildAt(2);
			rb.setChecked(true);
			etNumFirst.setText(_connectorSelected.get_fatherCardenalityLeft());
			etNumLast.setText(_connectorSelected.get_fatherCardenalityRight());
		}

	}

	private void setPrevioHijo(String tipo) {
		RadioGroup rgCardinality = (RadioGroup)findViewById(R.id.radio_group_hijo);
		RadioButton rb;

		EditText etSingleNum = (EditText)findViewById(R.id.et_single_numb_hijo);
		EditText etNumAsterisco = (EditText)findViewById(R.id.et_numb_asterisco_hijo);
		EditText etNumFirst = (EditText)findViewById(R.id.et_numb_numb_first_hijo);
		EditText etNumLast = (EditText)findViewById(R.id.et_numb_numb_last_hijo);

		if (_connectorSelected.get_childCardenalityLeft().equals(_connectorSelected.get_childCardenalityRight()))
		{
			rb = (RadioButton)rgCardinality.getChildAt(0);
			rb.setChecked(true);
			etSingleNum.setText(_connectorSelected.get_childCardenalityRight());
		}else if (_connectorSelected.get_childCardenalityRight().equals("*")){
			rb = (RadioButton)rgCardinality.getChildAt(1);
			rb.setChecked(true);
			etNumAsterisco.setText(_connectorSelected.get_childCardenalityLeft());
		}else{
			rb = (RadioButton)rgCardinality.getChildAt(2);
			rb.setChecked(true);
			etNumFirst.setText(_connectorSelected.get_childCardenalityLeft());
			etNumLast.setText(_connectorSelected.get_childCardenalityRight());
		}

	}

	private void setOnCLickGuardar() {
		Button btGuardar = (Button)findViewById(R.id.bt_guardar);
		btGuardar.setOnClickListener(new Button.OnClickListener() {
			public void onClick(View v) {				
				try{
					if (!first_hijo.getText().toString().equals("")&&!last_hijo.getText().toString().equals("")&&
							!first_padre.getText().toString().equals("")&&!last_padre.getText().toString().equals("")){
						_connectorSelected.set_childCardenalityLeft(first_hijo.getText().toString());
						_connectorSelected.set_childCardenalityRight(last_hijo.getText().toString());
						_connectorSelected.set_fatherCardenalityLeft(first_padre.getText().toString());
						_connectorSelected.set_fatherCardenalityRight(last_padre.getText().toString());
						EditarCardinalidad.this.dismiss();
					}else
						throw new CardinalityException();
				}catch(CardinalityException ce){
					alert(ce.getMessage());
				}
			}
		});
	}
}
